package proto

import (
	"fmt"
	"net"
	"server/public"
	"time"
)

/*
*  -*- coding:utf8 -*-
*
*  TCP 通信相关实现
*
*  @Author  : chengms
*  @WebSite : chengms.com
*  @Email   : chengms2012@163.com
*  @TIME    : 2020/8/23 下午6:13
*  @File    : client.go
 */

type ClientConn struct {
	Conn      net.Conn
	UserId    int
	UserName  string
	Writer    *CommandWriter
	Reader    *CommandReader
	Heartbeat time.Time
}

func (c *ClientConn) SendMsg(sendCmd *SendCommand, msg interface{}) error {
	//data, _ := public.Jsonit.MarshalToString(msg)
	//sendCmd.Data = append(sendCmd.Data, data)
	_ = sendCmd.SetSendCmd(msg)

	SendBuf, _ := public.Jsonit.MarshalToString(sendCmd)

	CmdBuf := SendBuf + ProtocolSep + "\n"
	length := fmt.Sprintf("%08x", len(CmdBuf))

	//public.Loggerf.Debugf("SendLength: %d, data: %v", length, CmdBuf)
	CmdBuf = length + CmdBuf

	err := c.Writer.Write(CmdBuf)

	return err
}
